<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
hello
defineProperty
<script>
  // "use strict";
  console.log('hello')

  //1
  let myCar = new Object();
  myCar.make = "Ford";
  myCar.model = "Mustang";
  myCar.year = 1969;
  console.group(myCar)
  console.log(myCar["make"])

  let myObj = new Object(),
    str = "myString",
    rand = Math.random(),
    obj = new Object();

  myObj.type = "Dot syntax";
  myObj["date created"] = "String with space";
  myObj[str] = "String value";
  myObj[rand] = "Random Number";
  myObj[obj] = "Object";
  myObj[""] = "Even an empty string";

  console.log(myObj);

  for (let i in myObj) {
    if (myObj.hasOwnProperty(i)) {
      let result = i + '  ' + obj[i]
      console.log(result)
    }
  }
  // console.error(myObj.hasOwnProperty)
  console.group('枚举对象属性')
  console.group('1.for in')
  for (let tmp in myObj) {
    console.log(tmp)
  }
  console.groupEnd()
  console.group('2.Object.keys(o)')
  console.log(Object.keys(myObj))
  console.groupEnd()
  console.group('3.Object.getOwnPropertyNames(o)')
  console.log(Object.getOwnPropertyNames(myObj))
  console.groupEnd()
  console.groupEnd()


  console.group('创建对象')
  console.group('1.使用对象初始化器')
  let obj1 = {
    property_1: '1',   // property_# 可以是一个标识符...
    2: 2,   // 或一个数字...
    ["property" + 3]: '2',  //  或一个可计算的key名...
  }; // 或一个字符串
  console.log(obj1)
  console.groupEnd()

  console.group('2.使用构造函数 new function->构造函数')
  function obj2(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }

  let myCar1 = new obj2("Eagle", "Talon TSi", 1993);
  console.log(myCar1)
  myCar1.color = "black";
  console.log(new obj2())
  obj2.prototype.color = null;
  console.log(myCar1)
  console.log(new obj2())
  console.groupEnd()

  console.group('3.使用 Object.create 方法->对象初始化-》crate')
  console.group('4.new object')
  let Animal = {
    type: "Invertebrates", // Default value of properties
    displayType: function () {  // Method which will display type of Animal
      console.log(this.type);
    }
  }
  let animal1 = Object.create(Animal);
  animal1.displayType(); // Output:Invertebrates
  console.groupEnd()
  console.group('定义 getters 与 setters')
  let o = {
    a: 7,
    get b() {
      return this.a + 1;
    },
    set c(x) {
      this.a = x / 2
    }
  };
  o.c = 10
  console.log(o.b)
  console.groupEnd()
  console.group('删除属性')
  let myobj = new Object();
  myobj.a = 5;
  myobj.b = 12;
  //Removes the a property, leaving myobj with only the b property.
  delete myobj.a;
  console.log(myobj)
  console.groupEnd()
  console.group('getter setter')
  const test = {
    a: 1,
    value: '1',
    get b() {
      return 2;
    },
    set b(val) {
      this.a = val
    },
    get x() {
      return this.value
    },
    set x(val) {
      this.value = val
    }
  }
  test.b = 100
  console.log(test.b)
  console.log(test.a)
  console.log(test.x)
  test.x = 100
  console.log(test.x)
  var obj2 = {
    x: 1,
    get a() {
      return this._b_;
    },
    set a(val) {
      this._b_ = val;
    }
  };

  Object.defineProperty(obj2, '_b_',{})
  obj2.a = 1;
  // console.log(obj2.a);//1
  //defineProperty
  Object.defineProperty(obj2, 'x', {writable: false})
  Object.defineProperty(obj2, 'y', {writable: false})
  obj2.x = 100;
  console.log('x',obj2.x);//1
  console.log(Object.keys(obj2))
  // console.log(obj2._b_);//1
  console.groupEnd()
  console.groupEnd()

  // console.log(myCar.noProperty)
</script>
</body>
</html>
